<?php

declare(strict_types=1);


namespace app\controller\member;

use app\controller\admin\AdminBaseController;
use app\model\admin\MiniUser;
use app\model\admin\Order;
use app\model\admin\OrderItem;
use app\model\admin\Refund;
use app\Request;
use app\model\admin\SysMember;

class OrderController extends AdminBaseController
{
    public function getOrderList(Request $request)
    {
        $params = $request->param();
        $user_id = $request->create_by;
        $info = SysMember::find($user_id);

        $list = Order::with(['orderItem' => function ($q) {
            $q->with(['refund' => function ($q) {
                $q->where('is_del', 0);
            }]);
        },  'user'])->where([
            "operator_id" => $user_id
        ])->scope('where', $params)
            ->order('id', 'desc')->paginate([
                'list_rows' => $this->limit,
                'page' => $this->page,
                'var_page' => $this->pageVar
            ]);
        return successPage($list);
    }
    public function getRefundOrderList(Request $request)
    {
        $params = $request->param();
        $user_id = $request->create_by;
        // $list = OrderItem::with(['order', 'user', 'refund' => function ($q) {
        //     $q->where('is_del', 0)->order('id', 'desc');
        // }])
        //     ->where([
        //         "operator_id" => $user_id
        //     ])
        //     ->where('refund_status', "<>", 0)
        //     ->scope('where', $params)
        //     ->order('id', 'desc')->paginate([
        //         'list_rows' => $this->limit,
        //         'page' => $this->page,
        //         'var_page' => $this->pageVar
        //     ]);
        $user_ids = MiniUser::where('op_id', $user_id)->column('id');
        $list = Refund::scope('where', $params)
            ->whereIn('user_id', $user_ids)
            ->where('is_del', 0)->with([
                "order", 'orderItem', 'user'
            ])->order('id', 'desc')->paginate([
                'list_rows' => $this->limit,
                'page' => $this->page,
                'var_page' => $this->pageVar
            ]);
        return successPage($list);
    }
}
